﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrabalhoLAP.Controller;

namespace TrabalhoLAP.Views.Cadastro.Professor
{
    public partial class AlterarProfessor : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void consultarProfessor_Click(object sender, EventArgs e)
        {
            DataSet dataset = new DataSet();
            MaintainProfessor maintainProfessor = new MaintainProfessor();
            ArrayList Professors = new ArrayList();
            string statusRegistro = ckbStatusRegistro.Checked == true? "1" : "0";


            Professors = maintainProfessor.consultarProfessor(txbNomeProfessor.Text.ToString(), txbCodProfessor.Text.ToString(), txbLoginProfessor.Text.ToString(), statusRegistro);

            if (dataset != null)
            {
                gdwResultadoConsulta.DataSource = Professors;
                gdwResultadoConsulta.DataBind();
                ViewState["ListaProfessors"] = Professors;
            }
            else
            {
                Response.Write("<script>alert('Erro ao obter os dados');</script>");
            }
        }

        protected void gdwResultadoConsulta_RowEditing(object sender, GridViewEditEventArgs e)
        {
            if (ViewState["ListaProfessors"] != null)
            {
                gdwResultadoConsulta.EditIndex = e.NewEditIndex;
                gdwResultadoConsulta.DataSource = ViewState["ListaProfessors"];
                gdwResultadoConsulta.DataBind();
            }
        }

        protected void gdwResultadoConsulta_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            DataSet dataset = new DataSet();
            MaintainProfessor mta = new MaintainProfessor();

            Label codProfessor     = gdwResultadoConsulta.Rows[e.RowIndex].FindControl("gdvLblCodProfessor")   as Label;
            TextBox nomeProfessor  = gdwResultadoConsulta.Rows[e.RowIndex].FindControl("gdvTxbNomeProfessor")  as TextBox;
            Label loginProfessor   = gdwResultadoConsulta.Rows[e.RowIndex].FindControl("gdvlblLoginProfessor") as Label;
            TextBox senhaProfessor = gdwResultadoConsulta.Rows[e.RowIndex].FindControl("gdvTxbSenhaProfessor") as TextBox;
            CheckBox CkbStatusRegistro = gdwResultadoConsulta.Rows[e.RowIndex].FindControl("gdvCkbStatusRegistro") as CheckBox;
            string statusRegistro = CkbStatusRegistro.Checked == true ? "1" : "0";

            if (mta.atualizarProfessor(codProfessor.Text, nomeProfessor.Text, loginProfessor.Text, senhaProfessor.Text, statusRegistro) != false)
            {
                Response.Write("<script>alert('Dados atualizados com sucesso');</script>");
                consultarProfessor_Click(null, null);
            }
            else
            {
                gdwResultadoConsulta.DataSource = ViewState["ListaProfessors"];
                gdwResultadoConsulta.DataBind();
                Response.Write("<script>alert('Erro ao atualizar os dados');</script>");
            }
            
            gdwResultadoConsulta.EditIndex = -1;
        }

        protected void gdwResultadoConsulta_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gdwResultadoConsulta.EditIndex = -1;
            gdwResultadoConsulta.DataSource = ViewState["ListaProfessors"];
            gdwResultadoConsulta.DataBind();
        }
    }
}